package cn.itsource.basic.publicController;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.user.service.WxuserService;
import cn.itsource.user.service.LogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LogininfoService logininfoService;

    @Autowired
    private WxuserService wxuserService;

    //账号登录 接口
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody Map<String, String> map) {
        try {
            return logininfoService.accountLogin(map);
        } catch (BusinessException b) {
            b.printStackTrace();
            return new AjaxResult(false, b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "服务器繁忙，请稍后再试【500】");
        }
    }

    //微信扫码登录接口
    @PostMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code) {
        try {
            return logininfoService.wechatLogin(code);
        } catch (BusinessException b) {
            b.printStackTrace();
            return new AjaxResult(false, b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "服务器繁忙，请稍后再试【500】");
        }
    }

    //微信电话绑定
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map) {
        try {
            return logininfoService.wechatBinder(map);
        } catch (BusinessException b) {
            b.printStackTrace();
            return new AjaxResult(false, b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "服务器繁忙，请稍后再试【500】");
        }
    }
}
